// VBConversions Note: VB project level imports
using System.Data;
using DevExpress.XtraEditors.Controls;
using System.Diagnostics;
using System.Xml.Linq;
using DevExpress.XtraEditors.Repository;
using System.Collections.Generic;
using System.Data.SqlClient;
using Microsoft.VisualBasic;
using System.Configuration;
using System.Collections;
using DevExpress.XtraEditors;
using System.Data.Sql;
using Microsoft.SqlServer;
using System.Windows.Forms;
using System;
using DevExpress.XtraReports;
using Microsoft.SqlServer.Management.Smo;
using DevExpress;
using System.Linq;
using System.IO;
// End of VB project level imports


namespace SCIEF.SysBFW.CommonExt
{
	public class TextWriterTraceListener : System.Diagnostics.TextWriterTraceListener
	{
		
#region  Field
		const string CST_DATELINE_FORMAT = " ******** {0} ******** ";
		private bool _logTime;
#endregion
		
#region  New
		/// <summary>
		/// Initializes a new instance of the System.Diagnostics.TextWriterTraceListener class with System.IO.TextWriter as the output recipient.
		/// </summary>
		/// <remarks></remarks>
		public TextWriterTraceListener()
		{
		}
		/// <summary>
		/// Initializes a new instance of the System.Diagnostics.TextWriterTraceListener class, using the stream as the recipient of the debugging and tracing output.
		/// </summary>
		/// <param name="stream">A System.IO.Stream that represents the stream the System.Diagnostics.TextWriterTraceListener writes to.</param>
		/// <remarks></remarks>
		public TextWriterTraceListener(System.IO.Stream stream) : base(stream)
		{
		}
		/// <summary>
		/// Initializes a new instance of the System.Diagnostics.TextWriterTraceListener class with the specified name, using the stream as the recipient of the debugging and tracing output.
		/// </summary>
		/// <param name="stream">A System.IO.Stream that represents the stream the System.Diagnostics.TextWriterTraceListener writes to.</param>
		/// <param name="name">The name of the new instance.</param>
		/// <remarks></remarks>
		public TextWriterTraceListener(System.IO.Stream stream, string name) : base(stream, name)
		{
		}
		/// <summary>
		/// Initializes a new instance of the System.Diagnostics.TextWriterTraceListener class using the specified writer as recipient of the tracing or debugging output.
		/// </summary>
		/// <param name="writer">A System.IO.TextWriter that receives the output from the System.Diagnostics.TextWriterTraceListener.</param>
		/// <remarks></remarks>
		public TextWriterTraceListener(System.IO.TextWriter writer) : base(writer)
		{
		}
		/// <summary>
		/// Initializes a new instance of the System.Diagnostics.TextWriterTraceListener class with the specified name, using the specified writer as recipient of the tracing or debugging output.
		/// </summary>
		/// <param name="writer">A System.IO.TextWriter that receives the output from the System.Diagnostics.TextWriterTraceListener.</param>
		/// <param name="name">The name of the new instance.</param>
		/// <remarks></remarks>
		public TextWriterTraceListener(System.IO.TextWriter writer, string name) : base(writer, name)
		{
		}
		/// <summary>
		/// Initializes a new instance of the System.Diagnostics.TextWriterTraceListener class, using the file as the recipient of the debugging and tracing output.
		/// </summary>
		/// <param name="fileName">The name of the file the System.Diagnostics.TextWriterTraceListener writes to.</param>
		/// <remarks></remarks>
		public TextWriterTraceListener(string fileName) : base(fileName)
		{
		}
		/// <summary>
		/// Initializes a new instance of the System.Diagnostics.TextWriterTraceListener class with the specified name, using the file as the recipient of the debugging and tracing output.
		/// </summary>
		/// <param name="fileName">The name of the file the System.Diagnostics.TextWriterTraceListener writes to.</param>
		/// <param name="name">The name of the new instance.</param>
		/// <remarks></remarks>
		public TextWriterTraceListener(string fileName, string name) : base(fileName, name)
		{
		}
		/// <summary>
		/// Initializes a new instance of the System.Diagnostics.TextWriterTraceListener class with the specified name, using the file as the recipient of the debugging and tracing output.
		/// </summary>
		/// <param name="fileName">The name of the file the System.Diagnostics.TextWriterTraceListener writes to.</param>
		/// <param name="name">The name of the new instance.</param>
		/// <param name="logTime">True if this listener is to write date information.</param>
		/// <remarks></remarks>
		public TextWriterTraceListener(string fileName, string name, bool logTime) : this(fileName, name)
		{
			this.LogTime = logTime;
		}
#endregion
		
#region  Properties
		/// <summary>
		/// Gets or sets if the Trace Listener will write the time in the destination.
		/// </summary>
		/// <value></value>
		/// <returns>True if the listener will write the time; false otherwise</returns>
		/// <remarks></remarks>
		public bool LogTime
		{
			get
			{
				return this._logTime;
			}
			set
			{
				this._logTime = value;
			}
		}
#endregion
		
#region  Methods
		/// <summary>
		/// Writes a category name and a message to the listener you create when you implement the System.Diagnostics.TraceListener class,
		/// followed by a line terminator.
		/// </summary>
		/// <param name="message">A message to write.</param>
		/// <remarks>If LogTime = True then a line with the time information is also added.</remarks>
		public override void WriteLine(string message)
		{
			this.ConditionalWriteTime();
			base.WriteLine(message);
		}
		
		/// <summary>
		/// Writes a category name and a message to the listener you create when you implement the System.Diagnostics.TraceListener class,
		/// followed by a line terminator.
		/// </summary>
		/// <param name="message">A message to write.</param>
		/// <param name="category">A category name used to organize the output.</param>
		/// <remarks>If LogTime = True then a line with the time information is also added.</remarks>
		public override void WriteLine(string message, string category)
		{
			this.ConditionalWriteTime();
			base.WriteLine(message, category);
		}
		
		/// <summary>
		/// Writes a line with the current date information if date infor is different than last call.
		/// </summary>
		/// <remarks></remarks>
		// VBConversions Note: Former VB local static variables moved to class level.
		static DateTime ConditionalWriteTime_lastCallTime = default(DateTime);
		
		protected void ConditionalWriteTime()
		{
			// static DateTime lastCallTime = default(DateTime); VBConversions Note: Static variable moved to class level and renamed ConditionalWriteTime_lastCallTime. Local static variables are not supported in C#.
			DateTime currentTime = GetNowMinusMilliseconds();
			//MyBase.WriteLine(currentTime)
			if (!ConditionalWriteTime_lastCallTime.Equals(currentTime))
			{
				ConditionalWriteTime_lastCallTime = currentTime;
				if (this.LogTime)
				{
					base.WriteLine(string.Format(CST_DATELINE_FORMAT, currentTime));
				}
			}
		}
		
		/// <summary>
		/// Gets the current Date and Time excluding the milliseconds.
		/// </summary>
		/// <returns>The current Date and Time excluding the milliseconds.</returns>
		/// <remarks></remarks>
		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")]protected static DateTime GetNowMinusMilliseconds()
		{
			return DateTime.Now.AddMilliseconds(System.Convert.ToDouble(- DateTime.Now.Millisecond));
		}
#endregion
	}
	
}
